#include <Function For Localize.au3>
#include <Date.au3>


Global $INI_SECTION_NAME_OS = 'OS'

Global $GHOST_NAME = ''

Global $TOTAL_OS_COUNT = IniRead($INI_PATH, $INI_SECTION_NAME_OS, 'OSCountToBeRun', '0')

Global $TEMP_SECTION_RECORD_ERROR = 'TmpErrorSection'

Global $TEMP_KEY_RECORD_ERROR = 'TmpErrorKey'

If $TOTAL_OS_COUNT == 0 Then
	
	MsgBox('', '', 'OSCountToBeRun set with wrong value')
	
	Exit
	
EndIf

Global $Next_Running_OS_NUMBER = IniRead($INI_PATH, $INI_SECTION_NAME_OS, 'NextOSNumberToBeRun', '0')

If $Next_Running_OS_NUMBER == 0 Then
	
	MsgBox('', '', 'OSCountToBeRun set with wrong value')
	
	Exit
	
EndIf

If IniRead($INI_PATH, $INI_SECTION_NAME_OS, 'IsRunFromFisrst', 'true') == 'true' Then
	
	IniWrite($INI_PATH, $INI_SECTION_NAME_OS, 'IsRunFromFisrst', 'false')
	
	IniDelete($INI_PATH, "HasBeenShutDown")
	
	IniDelete($INI_PATH, $TEMP_SECTION_RECORD_ERROR)
	
	If Number($Next_Running_OS_NUMBER) <= Number($TOTAL_OS_COUNT) Then
		
		$GHOST_NAME = IniRead($INI_PATH, $INI_SECTION_NAME_OS, 'OS' & $Next_Running_OS_NUMBER, '0')
		
		$GHOST_FILE_PATH = $GHOST_FOLDER_PATH & $GHOST_NAME
		
		IniWrite($INI_PATH, $INI_SECTION_NAME_OS, 'NextOSNumberToBeRun', $Next_Running_OS_NUMBER + 1)
		
		GhsotOS()
		
		Exit
		
	EndIf
EndIf

If Number($Next_Running_OS_NUMBER) <= Number($TOTAL_OS_COUNT) Then
	
	IniDelete($INI_PATH, "HasBeenShutDown")
	
	Run(@ScriptDir & '\Localize.exe')
	
	Sleep(10000)
	
	If Not ProcessWaitClose('Localize.exe', 7200) Then
		
		ProcessClose('Localize.exe')
		
		ProcessClose('Prestopm.exe')
		
		ProcessClose('setup.exe')
		
		ScreenCaprureWindow($PERFORM_RESULT_FOLDER_PATH, 'ScriptError_TimeOut.jpg', WinGetHandle(''))
		
		WriteLog_($LOG_FILE_PATH , 'Script Error: ProcessWaitClose("Localize.exe", 7200) Time Out')
		
		#cs
		Local $errorCount = IniRead($INI_PATH, $TEMP_SECTION_RECORD_ERROR, $TEMP_KEY_RECORD_ERROR, '0')
		IniWrite($INI_PATH, $TEMP_SECTION_RECORD_ERROR, $TEMP_KEY_RECORD_ERROR, $errorCount + 1)
		If $errorCount <= 4 Then
			$GHOST_NAME = IniRead($INI_PATH, $INI_SECTION_NAME_OS, 'OS' & $Next_Running_OS_NUMBER, '0')
			$GHOST_FILE_PATH = $GHOST_FOLDER_PATH & $GHOST_NAME
			GhsotOS()
			Exit		
		EndIf
		#ce
		
	EndIf
	
	IniDelete($INI_PATH, $TEMP_SECTION_RECORD_ERROR)
	
	Sleep(5000)
	
	$GHOST_NAME = IniRead($INI_PATH, $INI_SECTION_NAME_OS, 'OS' & $Next_Running_OS_NUMBER, '0')
	
	IniWrite($INI_PATH, $INI_SECTION_NAME_OS, 'NextOSNumberToBeRun', $Next_Running_OS_NUMBER + 1)
	
	$GHOST_FILE_PATH = $GHOST_FOLDER_PATH & $GHOST_NAME
	
	GhsotOS()
	
	Exit
	
EndIf

Func GhsotOS()
	
	Run($GHOST_FOLDER_PATH & 'Ghost.exe')
	
	Sleep(5000)
	
	$title = "[CLASS:AutoIt v3 GUI]"
	
	WinWait($title, '', 60)
	
	ControlSetText($title, '', 28, $GHOST_FILE_PATH)
	
	Sleep(1000)
	
	$t =  FileGetTime($GHOST_FILE_PATH, 0)
	
	If Not @error Then
		
		$year = $t[0]
		
		$month = $t[1]
		
		$date = $t[2]
		
		Local $tNew = _Date_Time_EncodeSystemTime($month, $date, $year, 13, 13, 00)   ; @MON, @MDAY, @YEAR, @HOUR, @MIN, @SEC
		
		_Date_Time_SetLocalTime(DllStructGetPtr($tNew))
		
		Sleep(1000)
		
	EndIf
	
	ControlClick($title, '', 31)
	
EndFunc

If GetOSLanguage() == "Chinese_PRC" And IniRead($INI_PATH, 'HasBeenShutDown', 'HasBeenShutDown', '0') <> 'Yes' Then
	
	Run(@ScriptDir & '\Localize.exe')
	
	Sleep(10000)
	
	If Not ProcessWaitClose('Localize.exe', 7200) Then
		
		ProcessClose('Localize.exe')
		
		ProcessClose('Prestopm.exe')
		
		ProcessClose('setup.exe')
		
		ScreenCaprureWindow($PERFORM_RESULT_FOLDER_PATH, 'ScriptError_TimeOut.jpg', WinGetHandle(''))
		
		WriteLog_($LOG_FILE_PATH , 'Script Error: ProcessWaitClose("Localize.exe", 7200) Time Out')
		
		#cs
		Local $errorCount = IniRead($INI_PATH, $TEMP_SECTION_RECORD_ERROR, $TEMP_KEY_RECORD_ERROR, '0')
		IniWrite($INI_PATH, $TEMP_SECTION_RECORD_ERROR, $TEMP_KEY_RECORD_ERROR, $errorCount + 1)
		If $errorCount <= 4 Then
			$GHOST_FILE_PATH = 'E:\Ghost\TC_XPSP3_32BIT.GHO'
			GhsotOS()
			Exit		
		EndIf
		#ce
		
	EndIf
	
	Sleep(5000)
	
EndIf

IniDelete($INI_PATH, $TEMP_SECTION_RECORD_ERROR)

If $GHOST_NAME == ''  And IniRead($INI_PATH, 'HasBeenShutDown', 'HasBeenShutDown', '0') <> 'Yes' Then   ;All Localize OS has been Executed.
	
	$FileList = _FileListToArray($PERFORM_RESULT_FOLDER_PATH_, '*' , 2)
	
	If @error Then Exit
	
	For $t = 1 to $FileList[0]
		
		$path_perform = $PERFORM_RESULT_FOLDER_PATH_ & $FileList[$t] & '\'
		
		$path_correct = $CORRECT_RESULT_FOLDER_PATH_ & $FileList[$t] & '\'
		
		$FileList_ = _FileListToArray($path_perform, '*' , 1)
		
		If @error Then Exit
			
		For $x = 1 to $FileList_[0]
			
			If StringRight($FileList_[$x], 3) == 'pdf' Or StringRight($FileList_[$x], 3) == 'PDF' Then
				
				WriteLog_($LOG_FODLER_PATH & $FileList[$t] & '.log', 'Check ' & $FileList_[$x] & ': PASS')
				
			ElseIf StringRight($FileList_[$x], 3) == 'xps' Or StringRight($FileList_[$x], 3) == 'XPS' Then
				
				WriteLog_($LOG_FODLER_PATH & $FileList[$t] & '.log', 'Check ' & $FileList_[$x] & ': PASS')
				
			Else
				If CompareFile($path_perform & $FileList_[$x], $path_correct & $FileList_[$x]) Then
					
					WriteLog_($LOG_FODLER_PATH & $FileList[$t] & '.log', 'Check ' & $FileList_[$x] & ': PASS')
					
				Else
					
					WriteLog_($LOG_FODLER_PATH & $FileList[$t] & '.log', 'Check ' & $FileList_[$x] & ': ____FAIL')
					
				EndIf
				
			EndIf	
			
		Next
		
	Next
	
	IniWrite($INI_PATH, "HasBeenShutDown", 'HasBeenShutDown', 'Yes')
	
	Sleep(1000)
	
	Shutdown(5)
	
EndIf

If IniRead($INI_PATH, 'HasBeenShutDown', 'HasBeenShutDown', '0') == 'Yes' Then
	
	MsgBox('','','Test Finished! Check the log files')
	
EndIf

